<?php
declare (strict_types = 1);

namespace app\controller\backend\order;

use app\model\communal\OrderConsultModel;
use app\service\logic\common\order\OrderConsultService;
use app\validate\common\order\ConsultValidate;
use think\App;
use think\Request;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class ConsultController extends BackendController
{
    protected $service;

    public function __construct(App $app, OrderConsultService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 视图
     *
     * @return string
     */
    public function view1()
    {
        return $this->layout('backend/order/consult1', 'order_consult1');
    }

    /**
     * 退款视图
     *
     * @return string
     */
    public function refund()
    {
        return $this->layout('backend/finance/refund', 'finance_refund');
    }

    /**
     * 退款审核
     *
     * @param Request $request
     * @return Json
     */
    public function rr(Request $request)
    {
        $params = $request->only(['id', 'refund_status', 'refund_remark', 'refund_way', 'refund_amount']);
        $this->validate($params, ConsultValidate::class . '.rr');
        $params['admin_name'] = $request->middleware('admin')['name'];

        $result = $this->service->refundReview($params);

        if ($result) {
            return amis();
        } else {
            return amis([], '操作失败', 417);
        }
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, ConsultValidate::class . '.index');

        $result = $this->service->getComplexList($params, $search_fields, $search);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 显示资源
     *
     * @param $id
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read($id): Json
    {
        $result = $this->service->getSimpleDetail($id);
        return amis($result);
    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function update(Request $request): Json
    {
        $params = $request->only($this->service->getUpdateColumn());
        $this->validate($params, ConsultValidate::class . '.update');

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * 单项修改
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function item(Request $request): Json
    {
        $params = $request->only($this->service->getItemColumn());
        $this->validate($params, ConsultValidate::class . '.item');

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * 筛选 订单状态
     *
     * @return Json
     */
    public function status()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$statusOptions);
        return amis(['options' => $options]);
    }
}
